//
//  QpSiteLocalCollectionViewCell.m
//  Tour
//
//  Created by 才诗琦 on 16/3/11.
//  Copyright © 2016年 张中烨. All rights reserved.
//

#import "QpSiteLocalCollectionViewCell.h"
@interface QpSiteLocalCollectionViewCell ()<UITableViewDataSource,UITableViewDelegate>
{
    NSInteger _page;
}
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArr;
@end
@implementation QpSiteLocalCollectionViewCell
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        /**< 创建tableview */
        [self creatTableView];
        _page = 1;
        
        self.dataArr = [NSMutableArray array];
        /**< 刷新加载 */
        [self refreshUI];
    }
    return self;
}
#pragma mark -创建tableview
- (void)creatTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(-10, 0, self.contentView.frame.size.width - 10, self.contentView.frame.size.height) style:UITableViewStylePlain];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
    //    _tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
//    [_tableView NightWithType:UIViewColorTypeNormal];
    [self.contentView addSubview:_tableView];
    [_tableView registerClass:[QpCityDeatilsTableViewCell class] forCellReuseIdentifier:@"cell"];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QpCityDeatilsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    QpCityDetailModel *qp = _dataArr[indexPath.row];
    //    NSLog(@"%@",qp.title);
    // 图片
    [cell.photo sd_setImageWithURL:[NSURL URLWithString:qp.photo]placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
    // 标题
    cell.title.text = qp.title;
//    [cell.title NightWithType:UIViewColorTypeNormal];
    // 价格
    cell.price.text = [[qp.price stringByReplacingOccurrencesOfString:@"<em>" withString:@""] stringByReplacingOccurrencesOfString:@"</em>" withString:@""];
//    [cell.price NightWithType:UIViewColorTypeNormal];
    // 折扣
    cell.priceoff.text = qp.priceoff;
//    [cell.priceoff NightWithType:UIViewColorTypeNormal];
//    [cell NightWithType:UIViewColorTypeNormal];
    return cell;
}
#pragma mark - 点击cell
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //取消tableview 选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
  
    
    QpCityDetailModel *qp = _dataArr[indexPath.row];
    NSString *url = [NSString stringWithFormat:@"http://m.qyer.com/z/deal/%@/?source=app&client_id=qyer_ios&track_app_version=6.8.3&track_deviceid=CDA3EEEB-61BF-473E-AFFF-32B6332C9BBF&track_user_id=7328550", qp.cellid];
    self.pushweb(url);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 100 * 200 / 300 + 20;
}
-(void)setDataArr:(NSMutableArray *)dataArr{
    
    _dataArr = dataArr;


}

#pragma mark - local 数据处理
- (void)dataHandlerlocal
{
    NSString *string = [NSString stringWithFormat:@"http://open.qyer.com/qyer/discount/local_discount?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=%@&oauth_token=89dd34fbffaaa72503b4baa7a9d02812&order=2&page=%ld",self.strid, _page];
    NSString *url = @"&product_type=2410&time=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=CDA3EEEB-61BF-473E-AFFF-32B6332C9BBF&track_os=ios%25207.1.2&track_user_id=7328550&type=2&v=1";
    
    [AFNModel GETWithHandler:[NSString stringWithFormat:@"%@%@",string,url] completion:^(id result) {
       
        for (NSDictionary *dic in result[@"data"][@"list"]) {
                        QpCityDetailModel *q = [QpCityDetailModel modelWithDic:dic];
                        [self.dataArr addObject:q];
                        //            NSLog(@"%@",_travelArr);
                    }
                    [_tableView reloadData];
                    [self.tableView.mj_header endRefreshing];
                    [self.tableView.mj_footer endRefreshing];
                    

    }];

}
- (void)refreshUI
{
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 初值
        _page = 1;
        // 刷新数据
        [self dataHandlerlocal];
    }];
    self.tableView.mj_header = header;
    [header setTitle:@"正在玩命加载中" forState:MJRefreshStatePulling];
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page++;
        // 加载数据
        [self dataHandlerlocal];
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
